<?php

/**
 * @copyright 2011 by Aaron, Brice, Dan, Mike.  This program is part
 * of Hapinoy Project, which is free software. It comes with absolutely no
 * warranty. You can redistribute or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation (For
 * more information, see <http://www.gnu.org/licenses/).
 *
 * @version April 12, 2011
 *
 * @author Brice Rader
 */
/**
 * This is the database file.
 * It controls the image file to list all categories and
 * to add or delete a image into the database.
 * It is the only file that has the SQL queries and MySQL connections
 */
include_once('functions.php');

class dbImage {

    private $db;

    static function createTable() {
        //create table image
        $query = "
        create table images(
            imageID      int             NOT NULL AUTO_INCREMENT,
            fileName     varchar(255)    NOT NULL,
            mimeType     varchar(255)    NOT NULL,
            fileSize     int             NOT NULL,
            fileData     longblob        NOT NULL,
            constraint PK_Image PRIMARY KEY(imageID)
            );
";
        connectToDatabase();
        $result = mysql_query($query);
        if ($result == false) {
            echo mysql_error();
            return false;
        }
        mysql_close();
        //Previous query was successful.
        return true;
    }

    /**
     * Gets all of the images in the database
     * @return images
     */
    function getAllImages() {
        $query = "SELECT * FROM image";
        connectToDatabase( );
        $result = mysql_query($query);
        mysql_close();
        if ($result == false) {
            echo mysql_error( );
            return false;
        }

        //Query was successful.
        return $result;
    }

    /**
     *
     * @param image $c
     * @return new image added to database
     */
    static function addImage($i) {
        if (!$i instanceof Image) {
            echo ("Invalid arguement insertInto ( ) function<br />");
            return false;
        }
        //Compose the INSERT INTO query srting. Primary keyvalue is missing.
        //Database server automatically generates a unique ID for the primary
        //key.
        $query = sprintf("INSERT INTO image VALUES (null, '%s')",
                        $c->getImageName()
        );
        connectToDatabase( );
        $result = mysql_query($query);
        printf("Image added has ID %d\n<br />", mysql_insert_id());
        mysql_close( );
        if ($result == false) {
            echo mysql_error ( );
            return false;
        }
        //query was successful.
        return true;
    }

    /**
     *
     * @param contact $c
     * @return checks to see what contacts are in the database
     */
    function checkImageID($i) {
        if (!($i instanceof Image)) {
            return false;
        }
        $imageID = $i->getImageID();
        connectToDatabase();
        if (mysql_num_rows(mysql_query("SELECT * FROM image WHERE
                imageID=$imageID")) == false) {
            mysql_close();
            //nothing existsmysql_num_rows
            return false;
        } else {
            return true;
        }
    }

    /**
     *
     * @param $imageID
     * @return ID
     */
    function getImageID($imageID) {
        $i = new Image ();
        connectToDatabase();
        $result = mysql_query("SELECT * FROM image WHERE
                imageID=$imageID");
        mysql_close();
        if (!$result) {
            return false; //ID does not exist
        } else {
            $row = mysql_fetch_assoc($result);
            $i->setImageID($imageID);
            return $i;
        }
    }

    /**
     *
     * @param $ImageName
     * @return Image
     */
    function getImageName($imageName) {
        $p = new Image ();
        connectToDatabase();
        $result = mysql_query("SELECT * FROM image WHERE imageName=
                $imageName");
        mysql_close();
        if (!$result) {
            return false; //name does not exist
        } else {
            $row = mysql_fetch_assoc($result);
            $i->setImageName($imageName);
            return $i;
        }
    }

    /**
     * Delete's image from database
     * @param contact $c
     * @return categories in database
     */
    function deleteImageID($i) {
        if (!($i instanceof Image)) {
            echo ("Invalid argument deleteFrom( ) function<br />");
            return false;
        }

        //Compose the DELETE FROM query string.
        $query = sprintf("DELETE FROM image WHERE imageID = %d",
                        $c->getImageID()
        );
        connectToDatabase( );
        $result = mysql_query($query);
        mysql_close( );
        if ($result == false) {
            echo mysql_error( );
            return false;
        }

        //Query was successful.
        return true;
    }

}

?>
